<?php
/**
 * Description of add
 *
 * @author laimis
 */

class AddGallery extends  ActionBase {
    public $title = 'Gallery add ';
    public $imageUpLoude;
    public $galleryId;
    
    function saveGallery(){
        include_once VALIDATEUSER;
        if(addGalleryValid()){
            if($this->getGalleryService() != null){
                $gallery = new Gallery();
                //$account = $this->getAccount();
                $gallery->title =$_POST['title'];
                $gallery->description =$_POST['description'];
                $gallery->layout_id = 1;
                $this->galleryId = $this->galleryService->save($gallery);
                $gallery->id = $this->galleryId;
                $photoId = $this->savePhoto();
                if($photoId[1]){
                    $gallery->defoultPhotoId = $photoId[1];
                    $this->galleryService->save($gallery);
                }
                $_REQUEST['request']['action'] = 'view';
                $_REQUEST['request']['arg'] = $this->galleryId;
                new GalleryWrapper();
            }else{
                $_GET['error'] = 'Galerijos sukurti nepavyko, klaida sistemine, susisiekite su administacija';
                include_once  BASEURL.'web/layout.php';
            }
        }else {
            $_GET['error'] = 'Galerijos isšaugoti nepavyko dėl netinkamos informacijos...';
            include_once  BASEURL.'web/layout.php';
        }
    }

    public function isPhotosLaudedInTmp($tmpPhoto){
        $result = false;
        if(isset ($tmpPhoto) && $tmpPhoto['size'] != 0){
            $result = true;
        }
        return $result;
    }

    public function upLoudPhoto($photo,$file){
        if($this->imageUpLoude == null){
            include_once IMAGERESIZER;
            $this->imageUpLoude = new ImgUpLauder();
        }

        $originalphoto = 'photos/original/0_'.mktime().$file['name'];
        copy($file['tmp_name'],BASEURL.$originalphoto);
        $photo->original = $originalphoto;
        $photo->mini = $this->imageUpLoude->uploadPhotos(1,187,133,$file['tmp_name'],'photos/mini/',$file['type'], $crop=true);
        $photo->midi = $this->imageUpLoude->uploadPhotos(2,250,297,$file['tmp_name'],'photos/midi/',$file['type'], $crop=true);
        $photo->maxi = $this->imageUpLoude->uploadPhotos(3,900,595,$file['tmp_name'],'photos/maxi/',$file['type'], $crop=true);
        return $photo;
    }

    public function savePhoto(){
        include_once PHOTOENTITY;
        $photo = null;
        $photoId[] = null;
        if($this->getPhotoService() != null){
            if($this->isPhotosLaudedInTmp($_FILES['photo_1'])){
                $photo = new Photo();
                $photo = $this->upLoudPhoto($photo,$_FILES['photo_1']);
                $photo->gallery_id = $this->galleryId;
                array_push($photoId,$this->photoService->save($photo));
            }
            if($this->isPhotosLaudedInTmp($_FILES['photo_2'])){
                $photo2 = new Photo();
                $photo2 = $this->upLoudPhoto($photo2,$_FILES['photo_2']);
                $photo2->gallery_id = $this->galleryId;
                array_push($photoId,$this->photoService->save($photo2));
            }
            if($this->isPhotosLaudedInTmp($_FILES['photo_3'])){
                $photo3 = new Photo();
                $photo3 =  $this->upLoudPhoto($photo3,$_FILES['photo_3']);
                $photo3->gallery_id = $this->galleryId;
                array_push($photoId,$this->photoService->save($photo3));
            }
        }
        return $photoId;
    }

    public function AddGallery(){
        if($this->getAccount() == null){
            include_once  BASEURL.'web/layout.php';
        }else{
            $this->saveGallery();
        }
    }
}
?>
